/**
 * Copyright (C) 2008 aileron.cc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cc.aileron.web.sso.logic;

import java.net.URL;

import cc.aileron.commons.rest.RestClient;
import cc.aileron.web.sso.ApplicationContext;
import cc.aileron.web.sso.dao.SessionDao;
import cc.aileron.web.sso.dto.OnetimeToken;
import cc.aileron.web.sso.dto.SessionDto;
import cc.aileron.web.sso.resources.SessionResource;

import com.google.inject.Inject;

/**
 * SessionLogic　実態
 * @author Aileron
 *
 */
public class SessionLogicImpl implements SessionLogic
{
    /**
     * constractor injecton
     * @param dao
     */
    @Inject
    public SessionLogicImpl(
    		final SessionDao dao,
    		final ApplicationContext context,
    		final RestClient client)
    {
    	this.client = client;
    	this.context = context;
        this.dao = dao;
        dao.initDb();
    }
    
    /**
     * client
     */
    private final RestClient client;
    
    /**
     * dao
     */
    private final SessionDao dao ;
    
    /**
     * context
     */
    private final ApplicationContext context;

    /* (非 Javadoc)
     * @see cc.aileron.web.sso.logic.SessionLogic#convertOnetimeToken(cc.aileron.web.sso.dto.OnetimeToken)
     */
    @Override
    public SessionDto convertOnetimeToken(final OnetimeToken token)
    {
        if(token==null || token.tokenId == null)
        {
            return null;
        }
        
        final SessionDto dto = dao.findByToken(token);
        dao.cleanToken(token);
        return dto;
    }

    /* (非 Javadoc)
     * @see cc.aileron.web.sso.logic.SessionLogic#createSession(cc.aileron.web.sso.dto.SessionDto)
     */
    @Override
    public OnetimeToken createSession(final SessionDto dto)
    {
        dao.insertSession(dto);
        
        final OnetimeToken token = new OnetimeToken();
        token.id = dto.id;
        
        dao.insertToken(token);
        
        return token;
    }

    @Override
    public OnetimeToken reissueToken(final SessionDto dto)
    {
    	final SessionDto session = dao.findBySessionId(dto);
    	if(session==null)
    	{
    		return new OnetimeToken();
    	}
    	
        final OnetimeToken token = new OnetimeToken();
        token.id = session.id;
        
        dao.insertToken(token);
        return token;
    }

	@Override
	public SessionDto findBySessionId(final SessionDto dto) throws Exception 
	{
		if(context.isSelf())
		{
			return dao.findBySessionId(dto);
		}
		
		final SessionResource resource = client
			.doGet(new URL(context.singleSignOnServer()+dto.sessionId+".json"))
			.parseDocumentJsonToObject(SessionResource.class);
		
		if(resource!=null)
		{
			dto.userIdentifier = resource.userIdentifier;
		}
		
		return dto;
	}
}